package ui;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import components.PumpController;

public class ProfileLoader extends Dialog {

	public PumpController pc;
	
	protected Object result;
	protected Shell shlLoadProfile;
	
	private boolean bExitFlag = false;
	private Shell parent_shell;
	private Text text_Dose1;
	private Text text_Dose2;
	private Text text_Dose3;
	private Text text_Dose4;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public ProfileLoader(Shell parent, int style) {
		super(parent, style);
		setText("Confimation");
		parent_shell = parent;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open(PumpController _pc) {
		createContents();
		shlLoadProfile.open();
		shlLoadProfile.layout();
		Display display = getParent().getDisplay();	
		Point location = parent_shell.getLocation();		// Get the location of the parent. *SR
		shlLoadProfile.setLocation(location.x+68, location.y+26);
		
		pc = _pc;
		
		Group grpProfile = new Group(shlLoadProfile, SWT.NONE);
		grpProfile.setText("Profile");
		grpProfile.setBounds(10, 10, 160, 210);
		
		final Button btnLoad = new Button(shlLoadProfile, SWT.NONE);
		final Button btnProfile1 = new Button(grpProfile, SWT.RADIO);
		btnProfile1.setFont(SWTResourceManager.getFont("Times New Roman", 14, SWT.NORMAL));
		btnProfile1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnLoad.setEnabled(true);
				text_Dose1.setText("0.5");
				text_Dose2.setText("0.7");
				text_Dose3.setText("0.6");
				text_Dose4.setText("0.5");
			}
		});
		btnProfile1.setBounds(10, 31, 140, 21);
		btnProfile1.setText("No Exercise");
		
		final Button btnProfile2 = new Button(grpProfile, SWT.RADIO);
		btnProfile2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnLoad.setEnabled(true);
				text_Dose1.setText("0.5");
				text_Dose2.setText("0.7");
				text_Dose3.setText("0.6");
				text_Dose4.setText("0.5");
			}
		});
		btnProfile2.setFont(SWTResourceManager.getFont("Times New Roman", 14, SWT.NORMAL));
		btnProfile2.setText("Light Exercise");
		btnProfile2.setBounds(10, 77, 140, 21);
		
		final Button btnProfile3 = new Button(grpProfile, SWT.RADIO);
		btnProfile3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnLoad.setEnabled(true);
				text_Dose1.setText("0.4");
				text_Dose2.setText("0.6");
				text_Dose3.setText("0.5");
				text_Dose4.setText("0.4");
			}
		});
		btnProfile3.setFont(SWTResourceManager.getFont("Times New Roman", 14, SWT.NORMAL));
		btnProfile3.setText("Heavy Exercise");
		btnProfile3.setBounds(10, 123, 140, 21);
		
		Group grpDescription = new Group(shlLoadProfile, SWT.NONE);
		grpDescription.setText("Dosage Breakdown");
		grpDescription.setBounds(176, 10, 168, 210);
		
		Label lblDose1 = new Label(grpDescription, SWT.NONE);
		lblDose1.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		lblDose1.setBounds(10, 42, 76, 28);
		lblDose1.setText("12AM-4AM:");
		
		Label lblDose2 = new Label(grpDescription, SWT.NONE);
		lblDose2.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		lblDose2.setText("4AM-6AM:");
		lblDose2.setBounds(10, 76, 76, 28);
		
		Label lblDose3 = new Label(grpDescription, SWT.NONE);
		lblDose3.setText("6AM-10AM:");
		lblDose3.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		lblDose3.setBounds(10, 110, 76, 28);
		
		Label lblDose4 = new Label(grpDescription, SWT.NONE);
		lblDose4.setText("10AM-12AM:");
		lblDose4.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		lblDose4.setBounds(10, 144, 76, 28);
		
		text_Dose1 = new Text(grpDescription, SWT.BORDER);
		text_Dose1.setEditable(false);
		text_Dose1.setBounds(92, 41, 66, 21);
		
		text_Dose2 = new Text(grpDescription, SWT.BORDER);
		text_Dose2.setEditable(false);
		text_Dose2.setBounds(92, 75, 66, 21);
		
		text_Dose3 = new Text(grpDescription, SWT.BORDER);
		text_Dose3.setEditable(false);
		text_Dose3.setBounds(92, 109, 66, 21);
		
		text_Dose4 = new Text(grpDescription, SWT.BORDER);
		text_Dose4.setEditable(false);
		text_Dose4.setBounds(92, 143, 66, 21);
		
		//Button btnLoad
		btnLoad.setEnabled(false);
		btnLoad.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				
			}
		});
		btnLoad.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				// Set the return value
				if (btnProfile1.getSelection() == true) { pc.getInsulinManager().setCurrentProfile("No Exercise"); }
				else if (btnProfile2.getSelection() == true) { pc.getInsulinManager().setCurrentProfile("Light Exercise"); }
				else if (btnProfile3.getSelection() == true) { pc.getInsulinManager().setCurrentProfile("Heavy Exercise"); }
				
				// Close the popup
				bExitFlag = true;
			}
		});
		btnLoad.setBounds(269, 231, 75, 45);
		btnLoad.setText("Load");
		
		Button btnCancel = new Button(shlLoadProfile, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				bExitFlag = true;
			}
		});
		btnCancel.setText("Cancel");
		btnCancel.setBounds(186, 231, 75, 45);
		while (!shlLoadProfile.isDisposed() && bExitFlag == false) { // *SR
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		shlLoadProfile.dispose(); // *SR
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlLoadProfile = new Shell(getParent(), getStyle());
		shlLoadProfile.setSize(360, 284);
		shlLoadProfile.setText("Load Profile");

	}
}
